/*** 
This do-file produces panel A of table 'Consumer Spending on Debit 
and Credit Cards, by Income Quartile and Sector'.
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"
local category "Spending"

* Create required subfolders
cap mkdir "${root}/results/Spending"
cap mkdir "${root}/results/paper numbers"
cap mkdir "${root}/results/paper numbers/`category'"

*-------------------------------------------------------------------------------
* Load daily credit card spending in Jan 2020 
*-------------------------------------------------------------------------------

* Load annualized credit card spending in January 2020
project, uses("${root}/data/derived/NIPA/NIPA Table 2.3.5M Personal Consumption Expenditures by Major Type of Product.dta")
use "${root}/data/derived/NIPA/NIPA Table 2.3.5M Personal Consumption Expenditures by Major Type of Product.dta", clear

* Restrict to spending categories that appear on credit cards
gen in_affinity = 0
replace in_affinity = 1 if serieslabel == "Furnishings and durable household equipment"
replace in_affinity = 1 if serieslabel == "Recreational goods and vehicles"   
replace in_affinity = 1 if serieslabel == "Other durable goods"
replace in_affinity = 1 if serieslabel == "Food and beverages purchased for off-premises consumption"
replace in_affinity = 1 if serieslabel == "Clothing and footwear"
replace in_affinity = 1 if serieslabel == "Gasoline and other energy goods"
replace in_affinity = 1 if serieslabel == "Other nondurable goods"
replace in_affinity = 1 if serieslabel == "Transportation services"
replace in_affinity = 1 if serieslabel == "Recreation services"
replace in_affinity = 1 if serieslabel == "Food services and accommodations"
replace in_affinity = 1 if serieslabel == "Financial services and insurance"
replace in_affinity = 1 if serieslabel == "Other services"
keep if in_affinity == 1

* Collapse all categories
gisid seriescode year month
gcollapse (sum) value, by(year month)
label var value "Nominal Annualized Card Spending, in Millions"

* Output
sort year month
gisid year month

keep if year == 2020 & month == 1
assert _N == 1

* Calculate daily credit card spending in January 2020
scalar nipa_daily_spend = value[1] / 365 * 1e6  // converting annualized spending in millions to daily spending in dollars
di "Daily spending in January 2020: " (nipa_daily_spend)

*-------------------------------------------------------------------------------
* Prepare data for table 
*-------------------------------------------------------------------------------

* Load posted Affinity data
project, uses("${root}/data/web/data/Affinity - National - Daily.csv")
import delim using "${root}/data/web/data/Affinity - National - Daily.csv", asdouble clear

keep year month day spend_all spend_all_q1 spend_all_q2 spend_all_q3 spend_all_q4 
order year month day spend_all spend_all_q1 spend_all_q2 spend_all_q3 spend_all_q4 

gen date = mdy(month, day, year)
format date %td
order date

* Reshape long on quartile
rename spend_all spend_all_q0
greshape long spend_all_q, i(date) j(income_q)
replace income_q = . if income_q==0
gisid date income_q, mi

* Keep values from 2020 onward
keep if inrange(date, mdy(1,1,2020), mdy(12,31,2021))

* Merge income quartile shares
rename income_q income_quartile

* Load income shares file in Stata format
preserve
	project, uses("${root}/data/web/data/Affinity Income Shares - National - 2020.csv")
	import delimited "${root}/data/web/data/Affinity Income Shares - National - 2020.csv", clear
	
	keep income_quartile share_jan2020
	rename share_jan2020 share
	
	tempfile income_shares
	save `income_shares'
restore

merge m:1 income_quartile using `income_shares', assert(1 3) nogen
rename income_quartile income_q
replace share = 1 if income_q == .

* Change in spending since Jan 2020
gen change_since_jan_2020 = spend_all_q * share * nipa_daily_spend / 1E9


* Calculate each quartile's share of aggregate decline
gegen change_sum = sum(change_since_jan_2020) if !mi(income_q), by(date)
gen share_decline = change_since_jan_2020 / change_sum

* Mean in initial period 
gen initial_spending = 1 * share * nipa_daily_spend / 1E9

* Shifted change 
gen rescaled_change_since_jan_2020 = change_since_jan_2020 + initial_spending

*-------------------------------------------------------------------------------
* Get scalars for table   
*-------------------------------------------------------------------------------

* Set dates to show on table
local early_date = mdy(4, 14, 2020)
local middle_date = mdy(8, 14, 2020)
local late_date = mdy(12, 31, 2021) 

gen period = ""
replace period = "early" if date == `early_date'
replace period = "middle" if date == `middle_date'
replace period = "late" if date == `late_date'

keep period income_q change_since_jan_2020 share_decline initial_spending share
drop if period == ""
replace income_q = 0 if income_q == .

reshape wide change_since_jan_2020 share_decline, i(income_q) j(period) string

rename(change_since_jan_2020* share_decline*) (*_level *_share_decline)
rename(initial_spending share)(jan_level jan_share_decline)

reshape long early middle late jan, i(income_q) j(est) string
order income_q est early middle late jan 
drop if income_q == 0 & est == "_share_decline"
sort income_q est

keep income_q early middle late jan

*-------------------------------------------------------------------------------
* Export 
*-------------------------------------------------------------------------------

export excel "${root}/results/new_app_table_5_a.xlsx", sheet(new_app_table_5_a, replace)
project, creates("${root}/results/new_app_table_5_a.xlsx")
